package adapters;

import android.content.Context;
import android.content.DialogInterface;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.administrator.newxlmedical.R;

import java.util.List;

import beans.NewUserInfoBean;
import beans.UserRightsBean;
import butterknife.BindView;
import constants.Constants;
import custom.BaseRecyclerAdapter;

/**
 * Created by Administrator on 2018/5/9.
 */

public class UserRightsAdapter extends BaseRecyclerAdapter<UserRightsAdapter.UserRightsViewHolder> {

    private Context mContext;
    private LayoutInflater inflater;
    private List<UserRightsBean> list;
    private OnClickListener onClickListener;
    private NewUserInfoBean.DataBean userInfoBean;
    public UserRightsAdapter.OnClickListener getOnClickListener() {
        return onClickListener;
    }

    public void setOnClickListener(UserRightsAdapter.OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }
    public void setList(List<UserRightsBean> list) {
        this.list = list;
    }
    public UserRightsAdapter(Context mContext, List<UserRightsBean> list, NewUserInfoBean.DataBean userInfoBean) {
        this.mContext = mContext;
        this.list = list;
        inflater = LayoutInflater.from(mContext);
        this.userInfoBean = userInfoBean;
    }

    @Override
    public UserRightsViewHolder onCreateViewHolder(ViewGroup parent, final int viewType) {
        final UserRightsViewHolder viewHolder = new UserRightsViewHolder(inflater.inflate(R.layout.adapter_user_rights, parent, false));
        viewHolder.tvChange.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onClickListener.onChangeClickListener(viewHolder.getPosition());
            }
        });
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(UserRightsViewHolder viewHolder, int position) {
        int roleInfoId = userInfoBean.getRoleInfoId();
        UserRightsBean bean = list.get(position);
        if (TextUtils.isEmpty(bean.getNumber())) {
            viewHolder.tvNumber.setText("无");
        }else {
            viewHolder.tvNumber.setText(bean.getNumber());
        }

        if (TextUtils.isEmpty(bean.getName())) {
            viewHolder.tvName.setText("无");
        }else {
            viewHolder.tvName.setText(bean.getName());
        }

        if (TextUtils.isEmpty(bean.getDepartment())) {
            viewHolder.tvDepartment.setText("无");
        }else {
            viewHolder.tvDepartment.setText(bean.getDepartment());
        }

        if (TextUtils.isEmpty(bean.getRole())) {
            viewHolder.tvRole.setText("无");
        }else {
            viewHolder.tvRole.setText(bean.getRole());
        }

        if (TextUtils.isEmpty(bean.getWorkType())) {
            viewHolder.tvType.setText("无");
        }else {
            viewHolder.tvType.setText(bean.getWorkType());
        }

        viewHolder.tvTime.setText(bean.getTime());
        if (!bean.isEnable()) {
            viewHolder.tvState.setTextColor(mContext.getResources().getColor(R.color.emergency_open_red));
            viewHolder.tvState.setText("禁用");
        } else {
            viewHolder.tvState.setTextColor(mContext.getResources().getColor(R.color.table_head_text_color));
            viewHolder.tvState.setText("启用");
        }

        if (bean.getRoleInfoId() == 5 || bean.getRoleInfoId() == 0){
            viewHolder.tvChange.setTextColor(mContext.getResources().getColor(R.color.gray_3));
            viewHolder.tvChange.setEnabled(false);
        }else {
            viewHolder.tvChange.setTextColor(mContext.getResources().getColor(R.color.date_bg_color));
            viewHolder.tvChange.setEnabled(true);
        }

        if (roleInfoId == Constants.ROLE_MANAGE){
            if (bean.getRoleInfoId() == 2){
                viewHolder.tvChange.setTextColor(mContext.getResources().getColor(R.color.gray_3));
                viewHolder.tvChange.setEnabled(false);
            }else {
                viewHolder.tvChange.setTextColor(mContext.getResources().getColor(R.color.date_bg_color));
                viewHolder.tvChange.setEnabled(true);
            }
        }else if (roleInfoId == Constants.ROLE_HEAD_NURSE){
            if (bean.getRoleInfoId() == 3){
                viewHolder.tvChange.setTextColor(mContext.getResources().getColor(R.color.gray_3));
                viewHolder.tvChange.setEnabled(false);
            }else {
                viewHolder.tvChange.setTextColor(mContext.getResources().getColor(R.color.date_bg_color));
                viewHolder.tvChange.setEnabled(true);
            }
        }

        if (position % 2 == 0) {
            viewHolder.llTitle.setBackgroundColor(mContext.getResources().getColor(R.color.partLineBackground));
        } else {
            viewHolder.llTitle.setBackgroundColor(mContext.getResources().getColor(R.color.white));
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class UserRightsViewHolder extends BaseRecyclerAdapter.BaseViewHolder {
        @BindView(R.id.tv_adapter_user_rights_number)
        TextView tvNumber;
        @BindView(R.id.tv_adapter_user_rights_name)
        TextView tvName;
        @BindView(R.id.tv_adapter_user_rights_department)
        TextView tvDepartment;
        @BindView(R.id.tv_adapter_user_rights_role)
        TextView tvRole;
        @BindView(R.id.tv_adapter_user_rights_time)
        TextView tvTime;
        @BindView(R.id.tv_adapter_user_rights_state)
        TextView tvState;
        @BindView(R.id.tv_adapter_user_rights_change)
        TextView tvChange;
        @BindView(R.id.tv_adapter_user_rights_worktype)
        TextView tvType;
        @BindView(R.id.ll_user_rights_title)
        LinearLayout llTitle;

        public UserRightsViewHolder(View itemView) {
            super(itemView);
        }
    }

    public interface OnClickListener {
        void onChangeClickListener(int position);
    }
}
